<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:payment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:payment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:payment:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="left" prop="itemName" />
      <el-table-column label="项目编号" align="left" prop="itemCode" />
      <el-table-column prop="itemStatus" label="状态" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.itemStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="itemOrder" />
      <el-table-column label="符号" align="center" prop="sign">
        <template slot-scope="scope">
          <span v-if="scope.row.sign == 1">正数</span>
          <span v-else>负数</span>
        </template>
      </el-table-column>
      <el-table-column label="计算方式" align="center" prop="computeMode">
        <template slot-scope="scope">
          <span v-if="scope.row.computeMode == 1">百章清单</span>
          <span v-else-if="scope.row.computeMode == 2">求和</span>
          <span v-else-if="scope.row.computeMode == 3">百分比</span>
          <span v-else-if="scope.row.computeMode == 4">固定值</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:payment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:payment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="项目名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入项目名称" />
        </el-form-item>

        <el-form-item label="项目编号" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入项目编号" />
        </el-form-item>

        <el-form-item label="排　　序" prop="itemOrder">
          <el-input-number v-model="form.itemOrder" controls-position="right" :min="10" :step="10" style="width: 100%" />
        </el-form-item>

        <el-form-item label="符　　号" prop="sign">
          <el-select v-model="form.sign" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in signOptions.data"
              :key="item.code"
              :value="item.code"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="计算方式" prop="computeMode">
          <el-select v-model="form.computeMode" placeholder="请选择" style="width: 100%" @change="handleComputeModeChange">
            <el-option
              v-for="item in computeModeOptions.data"
              :key="item.code"
              :value="item.code"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="百章编号" prop="hundredItem" v-if="this.mode1Show">
          <el-select v-model="form.hundredItem" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in hundredChaptersOptions.data"
              :key="item.code"
              :value="item.code"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="求和项目" prop="sumItem" v-if="this.mode2Show">
          <el-select v-model="sumItemArray" placeholder="请选择" multiple collapse-tags style="width: 100%" @change="handleSumItemChange">
            <el-option
              v-for="item in paymentItemOptions"
              :key="item.itemCode"
              :value="item.itemCode"
              :label="item.itemName">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="百分比项目" prop="percentageItem" v-if="this.mode3Show">
          <el-select v-model="form.percentageItem" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in paymentItemOptions"
              :key="item.itemCode"
              :value="item.itemCode"
              :label="item.itemName">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="系　　数" prop="ratio" v-if="this.mode3Show">
          <el-input v-model="form.ratio" type="number" @keydown.native="handleKey" placeholder="请输入百分比系数" />
        </el-form-item>

        <el-form-item label="状　　态">
          <el-radio-group v-model="form.itemStatus">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备　　注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayment, getPayment, delPayment, addPayment, updatePayment, selectOptionsList } from "@/api/system/payment";
import signJson from '@/data/sign.json';
import computeModeJson from '@/data/computeMode.json';
import hundredChaptersJson from '@/data/hundredChapters.json';

export default {
  name: "Payment",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付项目表格数据
      paymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
      },
      // 求和项目选中值
      sumItemArray: [],
      // 表单校验
      rules: {
        itemName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        itemCode: [
          { required: true, message: "项目编号不能为空", trigger: "blur" }
        ],
        itemOrder: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        sign: [
          { required: true, message: "符号不能为空", trigger: "blur" }
        ],
        hundredItem: [
          { required: true, message: "百章编号不能为空", trigger: "blur" }
        ],
        sumItem: [
          { required: true, message: "求和项目不能为空", trigger: "blur" }
        ],
        percentageItem: [
          { required: true, message: "百分比项目不能为空", trigger: "blur" }
        ],
        ratio: [
          { required: true, message: "系数不能为空", trigger: "blur" }
        ],
      },
      // 符号下拉数据
      signOptions: signJson,
      // 计算方式下拉数据
      computeModeOptions: computeModeJson,
      // 支付项目下拉数据
      paymentItemOptions: [],
      // 百章下拉数据
      hundredChaptersOptions: hundredChaptersJson,
      // 计算方式 1-5
      mode1Show: false,
      mode2Show: false,
      mode3Show: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询支付项目列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        itemName: null,
        itemCode: null,
        itemStatus: "0",
        itemOrder: 10,
        sign: null,
        computeMode: null,
        hundredItem: null,
        sumItem: null,
        percentageItem: null,
        ratio: null,
        remark: undefined,
      };
      this.resetForm("form");
      this.mode1Show = false;
      this.mode2Show = false;
      this.mode2Show = false;
      this.paymentItemOptions = [];
      this.sumItemArray = [];
    },
    selectComputeMode(val) {
      console.log(val);
      if (val == 1) {
        this.mode1Show = true;
        this.mode2Show = false;
        this.mode3Show = false;
      } else if (val == 2) {
        this.mode1Show = false;
        this.mode2Show = true;
        this.mode3Show = false;
      } else if (val == 3) {
        this.mode1Show = false;
        this.mode2Show = false;
        this.mode3Show = true;
      } else {
        this.mode1Show = false;
        this.mode2Show = false;
        this.mode3Show = false;
      }
    },
    // 计算方式发生改变时触发
    handleComputeModeChange(val) {
      this.selectComputeMode(val);
    },
    // 求和项目发生改变时触发
    handleSumItemChange() {
      let sumItemValue = '';
      this.sumItemArray.forEach((item) => {
        sumItemValue += item + ',';
      })
      sumItemValue = sumItemValue.substring(0, sumItemValue.length - 1);
      this.form.sumItem = sumItemValue;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      selectOptionsList({itemCode: null}).then(response => {
        this.paymentItemOptions = response.data;
        this.open = true;
        this.title = "添加支付项目";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const itemId = row.itemId || this.ids
      getPayment(itemId).then(response => {
        this.form = response.data;
        this.selectComputeMode(this.form.computeMode);
        if (this.form.sumItem != '' && this.form.sumItem != null) {
          let sumItemValue = this.form.sumItem.split(",");
          sumItemValue.forEach((item) => {
            this.sumItemArray.push(item);
          });
        }
        selectOptionsList({itemCode: this.form.itemCode}).then(res => {
          this.paymentItemOptions = res.data;
          this.open = true;
          this.title = "修改支付项目";
        });

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.itemId != null) {
            updatePayment(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayment(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delPayment(itemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    handleKey(e) {
      let key = e.key
      // 不允许输入'e'和'.'
      if (key === 'e') {
        e.returnValue = false
        return false
      }
      return true
    }
  }
};
</script>

<style lang="scss">
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type='number'] {
    -moz-appearance: textfield;
  }
</style>
